package com.abigdreamer.wordpress.ui;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import com.abigdreamer.wordpress.newspider.FileUtil;
import com.abigdreamer.wordpress.newspider.ThreadTaskManager;
import com.zving.framework.data.QueryBuilder;

/**   
 * 
 * @author Darkness
 * @date 2013-6-2 下午05:23:05
 * @website http://www.abigdreamer.com 
 * @version V1.0   
 */
public class ArticleAnaly extends ThreadTaskManager {
	
	List<WordpressArticle> articles = new ArrayList<WordpressArticle>();
	
	int matchCount = 0;
	public void process() {
		
		List<File> files = FileUtil.getAllFile("E:\\gather\\dixcus\\files", "xml");
		System.out.println(files.size());
		addTasks(files);
		
		for (int i = 0; i < 100; i++) {
			ArticleAnalyProcessor fourmSpider = new ArticleAnalyProcessor(this);
			addProcesser(fourmSpider);
		}

		startThread();
	}
	
	@Override
	protected void afterFinished() {
		System.out.println("==========分析开始");
		System.out.println("==========分析结束，共" + articles.size() + "篇文章");

		System.out.println("matchCount:" + matchCount);
		
		long startTime = System.currentTimeMillis();
		
		String sql = "INSERT INTO WORDPRESS_ARTICLE (ID, article_id, TITLE, URL) VALUES(?, ?,?, ?)";
		QueryBuilder queryBuilder = new QueryBuilder(sql);
		queryBuilder.setBatchMode(true);
		int failCount = 0;
		for (WordpressArticle article : articles) {
			if(article == null) {
				failCount ++;
				System.out.println("存在 "+failCount+" 条空 数据");
				continue;
			}
			queryBuilder.add(getUUID(), article.getId(), article.getTitle(), article.getUrl());
			queryBuilder.addBatch();
		}
		queryBuilder.execute();
		System.out.println("cost:" + (System.currentTimeMillis()-startTime)/1000+"s");
	}
	
	private String getUUID() {
		return UUID.randomUUID().toString();
	}
}
